/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.milib.display.ui.checkbox {
	import com.googlecode.milib.base.IMLBroadcasterListener;
	import com.googlecode.milib.base.MLBroadcastClass;
	import com.googlecode.milib.data.value.MLBooleanValue;
	import com.googlecode.milib.display.service.MLButton;
	import com.googlecode.milib.display.service.MLMC;
	import com.googlecode.milib.vo.MLEventInfo;

	import flash.display.MovieClip;

	/**
	 * @author Marek Brun
	 */
	public class MCCheckBox extends MLBroadcastClass implements IMLBroadcasterListener {
		
		public const event_RequestChange : String = 'RequestChange';
		
		private var mlmc:MLMC;
		private var mc_btn : MovieClip;
		private var mc_anBird : MovieClip;
		private var model:MLBooleanValue;
		private var mlmcAnBird:MLMC;
		private var btn:MLButton;
		private var isAutoChange:Boolean;
		
		public function MCCheckBox(mc:MovieClip, isAutoChange:Boolean=true) {
			mlmc=MLMC.forInstance(mc);
			this.isAutoChange=isAutoChange;
			mc_btn=MovieClip(mc.getChildByName('btn'));
			mc_anBird=MovieClip(mc.getChildByName('anBird'));
			mc_anBird.mouseChildren = false;
			mlmcAnBird=MLMC.forInstance(mc_anBird);
			mlmcAnBird.mc.gotoAndStop(1);
			btn=mlmc.getMLButton('btn');
			btn.addListener(this);
			
			setModel(new MLBooleanValue(false));
		}
		
		public function setModel(bool:MLBooleanValue):void {
			if(model){
				if(model==bool){ return; }
				model.removeListener(this);
			}
			model=bool;
			model.addListener(this);
			draw();
		}
		
		public function getModel():MLBooleanValue {
			return model;
		}
		
		protected function draw():void {
			if(model.v){
				mlmcAnBird.playToByTotalFrames(1);
			}else{
				mlmcAnBird.playTo(1);
			}
		}
		
		public function get mc():MovieClip { return mlmc.mc; };
		
		override public function getPrivateVariables():Array {
			return [
				['isAutoChange', isAutoChange],
				['model', model]
			].concat(super.getPrivateVariables());
		}
		
//********************************************************************************************
//		events for CheckBox
//********************************************************************************************
		public function onEvent(ev:MLEventInfo):void {
			switch(ev.hero){
				case model:
					switch(ev.event) {
						case model.event_Changed:
							draw();
						break;
					}
				break;
				case btn:
					switch(ev.event) {
						case btn.event_Press:
							if(isAutoChange){
								model.switch_();
							}else{
								broadcastEvent(event_RequestChange);
							}
						break;
					}
				break;
			}
		}
		
	}
}
